﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.ModFlow.Domain.FileLayer;
namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
    public class HFBData:ICloneable
    {

        public HFBData()
        {

            this._hfbList = new List<HFBItem>();

            ////测试用临时代码

            //HFBItem item = new HFBItem();
            //item.ICOL1 = 1;
            //item.ICOL2 = 2;
            //item.IROW1 = 1;

            //item.IROW2 = 1;

            //item.Layer = 1;


          



            //this._hfbList.Add(item);

            //this._enabled = true;
        }


        [field: NonSerialized]
        public event EventHandler<SCColleconChangedEventArgs> SCCollecionChanged;

        public void AskCollectionChanged(SCColleconChangedEventArgs args)
        {
            if (SCCollecionChanged != null)
            {
                SCCollecionChanged(this, args);
            }
        }

        #region 属性
        
        bool _enabled = false;

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }




        List<HFBItem> _hfbList;

        public List<HFBItem> HfbList
        {
            get { return _hfbList; }
           
        }

        List<HFBItem> _hfbTempList = new List<HFBItem>();

        public List<HFBItem> HfbTempList
        {
            get { return _hfbTempList; }
            set { _hfbTempList = value; }
        }


        bool _haveTempData = false;

        public bool HaveTempData
        {
            get { return _haveTempData; }

        }


        #endregion

       /// <summary>
        /// 在所有屏障中查找是否存在所提供的一个屏障
       /// </summary>
       /// <param name="isHaveBarrier">存在标志</param>
       /// <param name="cell">所选单元</param>
       /// <param name="index">是第几个屏障</param>
       /// <returns></returns>
        public HFBItem GetBarrier(ref  bool isHaveBarrier, List<int[]> cell,ref  int index)
        {            
            HA.ModFlow.Domain.DomainLayer.HFBItem barrier = new HFBItem(1, 1, 1, 2, 1, 0.1);
            foreach (HFBItem item in this ._hfbList)
            {
                if ((item.IROW1 == cell[0][0] && item.ICOL1 == cell[0][1] && item.Layer == cell[0][2] && item.IROW2 == cell[1][0] && item.ICOL2 == cell[1][1] && item.Layer == cell[1][2]) || (item.IROW1 == cell[1][0] && item.ICOL1 == cell[1][1] && item.Layer == cell[1][2] && item.IROW2 == cell[0][0] && item.ICOL2 == cell[0][1] && item.Layer == cell[0][2]))
                {
                    isHaveBarrier = true;
                    barrier = item;                  
                    break;
                }
                index++;
            }
            if (!isHaveBarrier)
            {
                barrier.IROW1 = cell[0][0];
                barrier.ICOL1 = cell[0][1];
                barrier.Layer = cell[0][2];
                barrier.IROW2 = cell[1][0];
                barrier.ICOL2 = cell[1][1];                
            }
            return barrier;
        }
        /// <summary>
        /// 添加或删除屏障
        /// </summary>
        /// <param name="isAdd">添加标记</param>
        /// <param name="barrier">新屏障</param>
        /// <param name="cell">所选单元格</param>
        public List<HFBItem> AddOrDelBarrier(bool isAdd, HFBItem barrier, List<int[]> cell)
        {
            HFBItem bar = (HFBItem)barrier.Clone();
            bool b = false;
            int index = 0;
            bar = GetBarrier(ref b, cell, ref index);//是否存在一个屏障           
            if (isAdd)
            {
                if (b)
                {
                    this._hfbList.RemoveAt(index);
                    this._hfbList.Insert(index, barrier);
                }
                else
                {
                    this._hfbList.Add(barrier);
                }
            }
            else
            {
                if (b)
                {
                    this._hfbList.RemoveAt(index);
                }
                else
                {
                    //bar = barrier;
                }
            }
            List<HFBItem> arry = new List<HFBItem>();
            foreach (HFBItem item in this ._hfbList)
            {
                arry.Add((HFBItem) item.Clone());
            }
            return arry;
        }

        #region 方法

        public void ResetHFBDatas(List<HFBItem> hfbDatas)
        {
            this._hfbList.Clear();

            this._hfbList.AddRange(hfbDatas);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._hfbList, SCCollectionChangedType.RESET, SCType.BARRIER));


        }


        public void AddTempHFBItems(List<HFBItem> hfbItems)
        {
            this._hfbTempList.AddRange(hfbItems);

            this._haveTempData = true;
        }

        /// <summary>
        /// 将暂存的数据提供给核心，并清空对应的选项
        /// </summary>
        public void CommitTempHFBItems()
        {

            if (this._haveTempData)
            {

                this.ResetHFBDatas(this._hfbTempList);

                this._hfbTempList.Clear();

            }

            this._haveTempData = false;

        }




        public PHorizontalFlowBarrier ToPack()
        {
            PHorizontalFlowBarrier ph = new PHorizontalFlowBarrier();
            ph.PHFB.Clear();           
            foreach (HFBItem item in _hfbList)
            {
                PHFB phfb = new PHFB();
                phfb.Factor = item.Factor;
                phfb.Hydchr = item.Hydchr;
                phfb.ICOL1 = item.ICOL1;
                phfb.ICOL2 = item.ICOL2;
                phfb.IROW1 = item.IROW1;
                phfb.IROW2 = item.IROW2;
                phfb.Layer = item.Layer;               
                ph.PHFB.Add(phfb);
            }
            ph.NHFBNP = ph.PHFB.Count;

            return ph;
        }

        /// <summary>
        /// 方法有问题 保修改
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            HFBData obj = (HFBData)this.MemberwiseClone();
            obj._hfbList = new List<HFBItem>();

            foreach (HFBItem item in this._hfbList)
            {
                obj._hfbList.Add((HFBItem)item.Clone());
            }
      
            return obj;

        }

        /// <summary>
        /// 提供障碍物集合位置信息 每个int[]均表示ZYXYX网格位置 保加
        /// </summary>
        /// <returns></returns>
        public List<int[]> ToLocationList()
        {
            List<int[]> locations = new List<int[]>();
            foreach (HFBItem item in _hfbList)
            {
                locations.Add(item.ToZYXYX());
            }

            return locations;
        }

        #endregion
    }
}
